package com.example.examsystem2.controller;


import com.example.examsystem2.entity.EClass;
import com.example.examsystem2.entity.common.ResponseResult;
import com.example.examsystem2.service.EClassService;
import com.example.examsystem2.util.JsonUtils;
import com.example.examsystem2.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("exam_system")
public class EClassController {

    @Autowired
    private EClassService eClassService;
    ResponseResult responseResult = new ResponseResult();

    @RequestMapping("/class_list")
    public String question(){
        return "views/manage/class_list";
    }


    @RequestMapping("/get_all_class")
    @ResponseBody
    public String get_all_class(){
        List<EClass> all_class = eClassService.get_all_class();
        responseResult.setCode(200);
         responseResult.setMessage("修改成功");
         responseResult.setResult(all_class);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/add_class")
    @ResponseBody
    public String add_class(EClass eClass){
        eClass.setC_state(1);
        eClass.setC_creat_time(TimeUtils.getTime());
        int i = eClassService.add_class(eClass);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("添加成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/del_class_by_c_id")
    @ResponseBody
    public String del_class_by_c_id(int c_id){
        int i = eClassService.del_class_by_c_id(c_id);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("添加成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_class_user_by_c_id")
    public String get_class_user_by_c_id(int c_id, Model model){
        model.addAttribute("c_id",c_id);
        return "views/manage/class_user_list";
    }

    @RequestMapping("/get_class_user")
    @ResponseBody
    public String get_class_user(){
        List<EClass> class_user = eClassService.get_class_user();
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(class_user);
        return JsonUtils.getJson(responseResult);
    }
}

